package com.dataseek.iot.mysql.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dataseek.iot.core.util.StringUtil;
import com.dataseek.iot.mysql.entity.Device;
import com.dataseek.iot.mysql.entity.Dict;
import com.dataseek.iot.mysql.entity.ProductParams;
import com.dataseek.iot.mysql.mapper.DeviceMapper;
import com.dataseek.iot.mysql.service.IDeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cc
 * @since 2021-09-19
 */
@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements IDeviceService {


    @Autowired
    ProductParamsServiceImpl productParamsService;

    @Autowired
    DictServiceImpl dictService;

    /**
     * 更新设备状态
     * @param id
     * @param state
     */
    public void updateStateById(String id, String state){
        UpdateWrapper wrapper = new UpdateWrapper();
        if(Objects.nonNull(id)) {
            wrapper.eq("device_inner_id", id);
            wrapper.eq("state", state);
        }else {
            throw new IllegalArgumentException("设备id字段值为空");
        }
        this.update(wrapper);
    }

    /**
     * 更新设备状态
     * @param code
     * @param state
     */
    public void updateStateByCode(String code, String state){
        UpdateWrapper wrapper = new UpdateWrapper();
        if(Objects.nonNull(code)){
            wrapper.eq("device_code", code);
            wrapper.set("state", state);
        }else {
            throw new IllegalArgumentException("设备code字段值为空");
        }
        this.update(wrapper);
    }

    /**
     * 获取设备列表
     * @param id
     * @return
     */
    public List<Device> getDeviceByLineId(String id){
        QueryWrapper<Device> wrapper = new QueryWrapper<>();
        wrapper.eq("line_id", id);
        return this.list(wrapper);
    }

    /**
     * 根据线路code查找设备列表
     * @param code
     * @return
     */
    public List<Device> getDeviceBylineCode(String code){
        QueryWrapper<Device> wrapper = new QueryWrapper<>();
        String[] codes = code.split(",");
        for(int i=0; i< codes.length; i++){
            if(i==0){
                wrapper.like("line_code", codes[0]);
            }else{
                wrapper.or(true).like("line_code", codes[i]);
            }
        }
        wrapper.orderByDesc("state", "update_time", "used");
        return this.list(wrapper);
    }


    /**
     * 根据产品code 查找设备
     * @param code
     * @return
     */
    public List<Device> getDeviceByProductCode(String code){
        QueryWrapper<Device> wrapper = new QueryWrapper<>();
        wrapper.eq("product_code", code);
        return this.list(wrapper);
    }

    /**
     * 返回分页信息
     * @param code
     * @param current
     * @param size
     * @return
     */
    public Page<Device> getPageBylineCode(String code, int current, int size){
        QueryWrapper<Device> wrapper = new QueryWrapper<>();
        wrapper.like("line_code", code);
        List<String> des = new ArrayList<>();
        des.add("state");
        des.add("update_time");
        des.add("used");
        Page p = new Page();
        p.setDescs(des);
        p.setCurrent(current);
        p.setSize(size);
        return this.page(p, wrapper);
    }



    /**
     * 根据设备编码获取设备信息
     * @param code
     * @return
     */
    public Device getDeviceByCode(String code){
        QueryWrapper<Device> wrapper = new QueryWrapper<>();
        wrapper.eq("device_code", code);
        return this.getOne(wrapper);
    }

    /**
     * 获取设备关联的字段编码
     * @param code
     * @param isLow 首字母是否小写
     * @return
     */
    public List<String> getDictNames(String code, boolean isLow){
        Device d =this.getDeviceByCode(code);
        List<String> names = new ArrayList<>();
        if(Objects.isNull(d)){
            log.error("未查询到数据");
            return null;
        }
        List<ProductParams> params = productParamsService.getAllByProductId(d.getProductId());
        for(ProductParams param : params){
            Dict dict = dictService.getById(param.getDictId());
            String name = dict.getParamCode();
            if(isLow){
                name = StringUtil.lowCaseFirst(name);
            }
            names.add(name);
        }
        return names;
    }
}

